package com.ez.supervise.action.sys;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.commons.beanutils.BeanUtils;

import com.ez.supervise.action.base.BaseJsonAction;
import com.ez.supervise.core.Page;
import com.ez.supervise.model.sys.Role;
import com.ez.supervise.service.RoleService;


public class RoleAction extends BaseJsonAction {
    private static final long serialVersionUID = 8735170970520475073L;
    private Long id;
    private RoleService roleService;
    private Role role;
    private Page page;

    public void saveRole() {
	try {
	    if (role.isNew()) {
		Role saveRole = new Role();
		saveRole.setName(role.getName());
		saveRole.setMemo(role.getMemo());
		roleService.insert(saveRole);
	    } else {
		roleService.update(role);
	    }
	    outJsonString(RESULT_SUCCESS);
	} catch (Exception e) {
	    outJsonString(RESULT_FAIL);
	}
    }

    public String findAllRole() {
	page = new Page();
	page.setRoot(roleService.findAllRole());
	JsonConfig config = new JsonConfig(); 
	config.setExcludes(new String[] { "creator", "createTime", ",modifier", "modifyTime", "isDeleted", "new", });
	JSONObject jsonObj = JSONObject.fromObject(page, config);
	// System.out.println(jsonObj.toString());
	outJsonString(jsonObj.toString());
	return SUCCESS;
    }

    /**
     * @return
     */
    public String deleteRole() {
	try {
	    if (id != null && id > 0) {
		roleService.delete(id);
	    }
	    outJsonString(RESULT_SUCCESS);
	} catch (Exception e) {
	    outJsonString(RESULT_FAIL);
	}
	return SUCCESS;
    }

    /**
     * @return
     * @throws Exception
     */
    public String updateRole() throws Exception {
	String fieldName = getRequest().getParameter("fieldName");
	String fieldValue = getRequest().getParameter("fieldValue");
	String strRoleId = getRequest().getParameter("id");
	try {
	    if (id != null && id > 0) {
		Role saveRole = new Role();
		saveRole.setId(id);
		BeanUtils.setProperty(saveRole, fieldName, fieldValue);
		roleService.update(saveRole);
	    }
	    outJsonString(RESULT_SUCCESS);
	} catch (Exception e) {
	    outJsonString(RESULT_FAIL);
	}

	return SUCCESS;
    }

    public static void main(String[] args) {
	Role role = new Role();
	role.setId(1L);
	role.setName("linlinyu");
	JSONObject json = JSONObject.fromObject(role);

	System.out.println(json);

	JsonConfig config = new JsonConfig();
	config.setExcludes(new String[] { "creator", "createTime", ",modifier", "modifyTime", "isDeleted", "new", });
	
	JSONObject jsonObj = JSONObject.fromObject(role, config);
	System.out.println(jsonObj.toString());

    }

    public Long getId() {
	return id;
    }

    public void setId(Long id) {
	this.id = id;
    }

    public void setRoleService(RoleService roleService) {
	this.roleService = roleService;
    }

    public RoleService getRoleService() {
	return roleService;
    }

    public void setRole(Role role) {
	this.role = role;
    }

    public Role getRole() {
	return role;
    }
}
